﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Member
{
    public partial class MemberEditInfo :UserPage
    {
        BLL.CNVP_Users bll = new BLL.CNVP_Users();
        Model.CNVP_Users model = new Model.CNVP_Users();
        public string Province = "0", City = "0", Area = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ProvinceBind();
                DataInfo();
                #region js验证
                txtEmail.Attributes.Add("onFocus", "RegTipInfo('lbEmail','请输入Email．')");
                txtEmail.Attributes.Add("onBlur", "return RegCheck('txtEmail','lbEmail')");
                btUp.Attributes.Add("onClick", "return CheckMemberEditInfo()");
                #endregion
                ddlProvince.Attributes.Add("onchange", "ProvinceChange()");
            }
        }

        private void ProvinceBind()
        {
            IList<Model.CNVP_Province> model = new BLL.CNVP_Province().GetAll_CNVP_Province();
            foreach (Model.CNVP_Province p in model)
            {
                ddlProvince.Items.Add(new ListItem(p.Name, p.Code));
            }
        }

        public void DataInfo()
        {
            model = bll.GetSingle_CNVP_UsersById(this.CookieUserID);
            if (model != null)
            {
                lbUserName.Text = this.CookieString;
                var model_UserLevel=new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(model.UserLevelID);
                if (model_UserLevel != null)
                {
                    lbUserLevelID.Text = model_UserLevel.LevelName;
                }
                txtEmail.Text = model.Email;
                txtOtherText.Text = model.OtherText;
                txtMobilePhone.Text = model.MobilePhone;
                ddlProvince.SelectedValue = model.Province;
                Province = string.IsNullOrEmpty(model.Province)?"0":model.Province;
                City = string.IsNullOrEmpty(model.City) ? "0" : model.City;
                Area = string.IsNullOrEmpty(model.Area) ? "0" : model.Area;
                txtGetAddress.Text = model.GetAddress;
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                string Email = txtEmail.Text.Trim();
                if (!Public.IsEmail(Email) || string.IsNullOrEmpty(Email))
                {
                    MessageUtils.ShowPre("邮箱不能为空或格式不正确！");
                    return;
                }
                model.Email = Email;
                model.OtherText = txtOtherText.Text.Trim();
                model.UserName = this.CookieString;
                model.Province = ddlProvince.SelectedValue;
                model.City = Request.Form["ListCity"];
                model.Area = Request.Form["ListArea"];
                model.GetAddress = txtGetAddress.Text;
                model.MobilePhone = txtMobilePhone.Text;
                int rows = bll.UpdateByUserName(model);
                if (rows == 1)
                {
                    MessageUtils.ShowPre("修改成功！");
                }
                else
                {
                    MessageUtils.ShowPre("修改失败！");
                }
            }
            catch
            {
                MessageUtils.ShowPre("悲剧，出错了！");
            }
        }
    }
}